package com.cskaoyan.shiro;

import com.cskaoyan.utils.StringUtils;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 解决前后端分离session跨域问题
 */
public class CustomSessionManager extends DefaultWebSessionManager {
    @Override
    protected Serializable getSessionId(ServletRequest srequest, ServletResponse response) {
        HttpServletRequest request = (HttpServletRequest) srequest;
        String sessionId = request.getHeader("X-cskaoyan-mall-Admin-Token");
        // 如果sessionId不为null，使用该session
        if (!StringUtils.isEmpty(sessionId))
        {
            return sessionId;
        }

        String sessionId2 = request.getHeader("X-cskaoyan-mall-wx-Token");
        if (!StringUtils.isEmpty(sessionId2))
        {
            return sessionId;
        }

        return super.getSessionId(request, response);
    }
}
